package it.giuseppetrisciuoglio.lyrics.manager;

import it.giuseppetrisciuoglio.lyrics.adapter.ArtistaCanzone;
import it.giuseppetrisciuoglio.lyrics.persistence.entity.Errore;
import it.giuseppetrisciuoglio.lyrics.persistence.entity.ParserType;
import it.giuseppetrisciuoglio.lyrics.utils.AdapterUtilities;

import java.io.Serializable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class JMSManager {
	
	public static final String Q_SINGRING = "SingringVirgilio.Queue";
	public static final String Q_LYRICS_MUSICA = "LyricsMusica.Queue";
	public static final String Q_LYRICS_KEEPER = "LyricsKeeper.Queue";
	public static final String Q_AIRDAVE = "AirDave.Queue";
	public static final String Q_LYRICS_MANIA = "LyricsMania.Queue";
	public static final String Q_LYRICS_TIME = "LyricsTime.Queue";
	public static final String Q_WIKIA_LYRICS = "WikiaLyrics.Queue";
	public static final String Q_TEKSTY = "Teksty.Queue";
	public static final String Q_WIKITESTI = "WikiTesti.Queue";
	public static final String Q_LYRICSMODE = "LyricsMode.Queue";
	
	private static final String Q_ERROR = "Error.Queue";
	private static final String Q_RETRY_LYRICS = "RetryLyrics.Queue";
	
	private final transient Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private JmsTemplate jmsTemplate;
	
	@Autowired
	private AdapterUtilities adapterUtilities;
	
	@Value("${retry.enabled}")
	private boolean retryEnabled;
	
	public void sendRetry(ArtistaCanzone artistaCanzone){
		log.debug("sendRetry(): artistaCanzone={}", artistaCanzone);
		if (!retryEnabled) {
			log.debug("sendRetry(): retry is disabled.");
			return;
		}
		sendJMS(Q_RETRY_LYRICS, adapterUtilities.createXMLString(artistaCanzone));
	}
	
	public void sendJMS(ParserType parserType, final Serializable obj){
		sendJMS(getQueueName(parserType), obj);
	}
	
	private void sendJMS(String queue, final Serializable obj){
		log.debug("sendJMS(): queue={}, artistaCanzone={}", queue, obj);
		jmsTemplate.send(queue, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
			    ObjectMessage om = session.createObjectMessage();
			    om.setObject(obj);
			    return om;
			}
		});
	}
	
	public void sendError(Errore errore){
		log.debug("sendError(): errore={}", errore.getError());
//		sendJMS(Q_ERROR, errore);
	}
	
	private String getQueueName(ParserType parserType){
		switch (parserType) {
		case AIRDAVE:
			return Q_AIRDAVE;
		case LYRICSKEEPER:
			return Q_LYRICS_KEEPER;
		case LYRICSMANIA:
			return Q_LYRICS_MANIA;
		case LYRICSMUSICA:
			return Q_LYRICS_MUSICA;
		case LYRICSTIME:
			return Q_LYRICS_TIME;
		case SINGRING:
			return Q_SINGRING;
		case TEKSTY:
			return Q_TEKSTY;
		case WIKIALYRICS:
			return Q_WIKIA_LYRICS;
		case WIKITESTI:
			return Q_WIKITESTI;
		case LYRICSMODE:
			return Q_LYRICSMODE;
		default:
			return null;
		}
	}
}
